<template>
  <div class="home">
    <el-container class="home-container">
      <el-header>
        <div >
          <img src="../assets/sign.jpg" />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <el-aside width="isCollapse ? '64px' : '200px'">
          <div class="toogle-button" @click="toogleCollapse">|||</div>
          <!-- 左侧区域 -->
          <el-menu
            background-color="#333744"
            text-color="#fff"
            router
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            :default-active="activePath"
          >
            <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
              <template slot="title">
                <i :class="fontObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/' + subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="saveNavSate('/' + subItem.path)"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      fontObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧列表
    async getMenuList() {
      const { data: res } = await this.$http.get('/menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    toogleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    saveNavSate (activePath) {
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath = activePath
    }
  },
}
</script>

<style lang="less" scoped>
.home {
  height: 100%;
  .home-container {
    height: 100%;
    // 头部
    .el-header {
      background-color: #373d41;
      display: flex;
      justify-content: space-between;
      align-items: center;
      color: #fff;
      font-size: 20px;
      padding-left: 0;
      > div {
        display: flex;
        align-items: center;
        img{
          width:110px
        }
        span {
          margin-left: 20px;
        }
      }
    }
    .el-aside {
      background-color: #333744;
      .toogle-button {
        background-color: #63666f;
        font-size: 10px;
        letter-spacing: 0.2em;
        color: #fff;
        text-align: center;
        line-height: 24px;
        cursor: pointer;
      }
    }
    .el-main {
      background-color: #eaedf1;
    }
    .el-menu {
      border-right: none;
    }
  }
}
</style>